package com.m3839.sdk.pay.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.m3839.sdk.common.base.BaseBean;
import com.m3839.sdk.common.core.Constant;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:快爆支付配置
 *
 * @author 陈宝阳
 * @create 2020/8/6 15:03
 */
public class PayConfig extends BaseBean<PayConfig> implements Parcelable {

  /**
   "gameId": 9763,
   "gameName": "0811快爆SDK最新",
   "moneyName": "点券",
   "moneyRate": 2131,
   "channels": [
   "wechatH5",
   "alipay"
   ],
   "maintenanceContent": "支付维护中",
   "maintenanceStartTime": "2020-08-12 17:15:00",
   "maintenanceEndTime": "2020-08-12 17:15:02"
   */
  /**
   * 支付的方式
   */
  public List<String> payChannels;

  /**
   * 游戏币名称
   */
  public String moneyName;

  /**
   * 支付比例
   */
  public String moneyRate;

  /**
   * 联系方式
   */
  public String contact;

  /**
   * 支付维护文案
   */
  public String maintenanceContent;

  /**
   * 下单失败，需要展示（“重新查询订单”按钮）的支付方式
   */
  public List<String> payRetries;


  public PayConfig(){

  }

  protected PayConfig(Parcel in) {
    payChannels = in.createStringArrayList();
    moneyName = in.readString();
    moneyRate = in.readString();
    contact = in.readString();
    maintenanceContent = in.readString();
    payRetries = in.createStringArrayList();
  }

  public static final Creator<PayConfig> CREATOR = new Creator<PayConfig>() {
    @Override
    public PayConfig createFromParcel(Parcel in) {
      return new PayConfig(in);
    }

    @Override
    public PayConfig[] newArray(int size) {
      return new PayConfig[size];
    }
  };

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringList(payChannels);
    dest.writeString(moneyName);
    dest.writeString(moneyRate);
    dest.writeString(contact);
    dest.writeString(maintenanceContent);
    dest.writeStringList(payRetries);
  }

  @Override
  public PayConfig parseBean(JSONObject jsonObject) {
    if(jsonObject == null) {
      return null;
    }

    moneyRate = jsonObject.optString(Constant.HYKB_KEY_RESPONSE_MONEY_RATE);
    moneyName = jsonObject.optString(Constant.HYKB_KEY_RESPONSE_MONEY_NAME);
    contact = jsonObject.optString(Constant.HYKB_KEY_RESPONSE_PAY_CONTACT);

    payChannels = parseChannels(
            jsonObject.optJSONArray(Constant.HYKB_KEY_RESPONSE_PAY_CHANNELS));
    payRetries = parseChannels(
            jsonObject.optJSONArray(Constant.HYKB_KEY_RESPONSE_PAY_RETRIES));
    return this;
  }

  private static List<String> parseChannels(JSONArray jsonArray) {
    if (jsonArray == null) {
      return null;
    }
    List<String> tips = new ArrayList<>();
    int len = jsonArray.length();
    for (int i = 0; i < len; i++) {
      tips.add(jsonArray.optString(i));
    }
    return tips;
  }
}
